//
//  welcomeVC.swift
//  我的微博
//
//  Created by shmily on 15/7/30.
//  Copyright © 2015年 Stanford-IOS8-Swift-shmilyAshen. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeVC: UIViewController {

    ///图标按钮的约束属性,为了设置动画
    var constraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        addSubViews()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }

    //开始动画
    private func startAnimation() {
        constraint?.constant = -constraint!.constant - UIScreen.mainScreen().bounds.height
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
             self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //发送通知
                NSNotificationCenter.defaultCenter().postNotificationName(switchVCNotification, object: true)
        }
        
    }
    
    //MARK: - 布局子控件
    private func addSubViews() {
        view.addSubview(imgBack)
        view.addSubview(imgIconView)
        view.addSubview(lblWelcom)
        layoutSubviews()
    }
    //设置自动布局
    private func layoutSubviews() {
        //imgBack
        imgBack.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":imgBack]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":imgBack]))
        //imgIconView
        imgIconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: imgIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: imgIconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
        //获取底部约束
        constraint = view.constraints.last
        view.addConstraint(NSLayoutConstraint(item: imgIconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: imgIconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        //lblWelcome
        lblWelcom.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: lblWelcom, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: imgIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: lblWelcom, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: imgIconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    
    }
    
    //MARK: - 懒加载控件
    //背景色 imgBack
    lazy var imgBack: UIImageView = {
        let imgBac = UIImageView()
        imgBac.image = UIImage(named: "ad_background")
        return imgBac
        }()
    
    //头像控件 imgIcon
    lazy var imgIconView: UIImageView = {
        let imgIcon = UIImageView()
        imgIcon.layer.cornerRadius = 45
        imgIcon.layer.masksToBounds = true 
        imgIcon.sd_setImageWithURL(NSURL(string: (UserInfo.getUserInfo()?.avatar_large)!), placeholderImage: UIImage(named: "avatar_default_big"))
        return imgIcon
    }()
    
    //文本标签 lblWelcome
    lazy var lblWelcom: UILabel = {
        let lblWel = UILabel()
        lblWel.text = "欢迎归来"
        lblWel.sizeToFit()
        return lblWel
    }()
    


}
